﻿@{
    ViewBag.Title = "消息通知";
    Layout = "~/Views/Shared/_LayoutDualColumns.cshtml";
}

@using Global.Core.ExtensionMethod
@model Ducu.SRV.ViewModel.Message.IndexModel

@section PageHeader{
    <span class="glyphicon glyphicon-bell" aria-hidden="true"></span><span class="after-glyphicon">@ViewBag.Title</span>
}

@if (Model.Items.IsNullOrEmpty())
{
    <div>暂时还没有消息。</div>
}
else
{
    foreach (var item in Model.Items)
    {
        string unread_class = item.HasRead ? "panel-default" : "panel-primary";
        <div class="panel @unread_class">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-comment"></span><span style="margin-left: 10px;">@item.CreateTime.ToChinese()</span>
            </div>
            <div class="panel-body">
                @Html.Raw(item.Content)
            </div>
            <div class="panel-footer">
                @if (!item.HasRead)
                {
                    <label class="checkbox-inline"><input type="checkbox" value="@item.Id" name="message_known" />知道了</label>
                }
                @if (!string.IsNullOrEmpty(item.Reply))
                {
                    <a href="@item.Reply" class="btn btn-info btn-sm pull-right" data-as="reply">回复</a>
                }
                @Html.Partial("_ClearFloat")
            </div>
        </div>
    }
    @Html.Action("_Pager", "Shared", Model.Pager)

}
@Html.Partial("~/Views/Task/Shared/_HistoryContainer.cshtml")

@section RightPanel{
    <li>
        未读消息显示为粗体。
    </li>
    <li>
        勾选“知道了”，或者直接点击其目标链接，都会将该消息标记为已阅读。
    </li>
    <li>
        目前系统只在以下情形产生消息通知：
        <ul>
            <li>
                有人加入你的目标
            </li>
            <li>
                任务被验收/拒收/质疑
            </li>
        </ul>
    </li>
    <li>
        系统只保留最近100条消息。但和目标/任务相关的留言等内容不会被删除，仍然可以在各目标的<a>任务完成情况</a> 中看到。
    </li>
}

@section scripts{
    <script>
        $(document).ready(function () {
            $("[data-as='reply']").click(function (event) {
                var href = $(this).attr("href");
                var $hasKnownCheckbox = $(this).parent().find("input[type='checkbox']");
                if (href.substring(0, 14) == "/Task/_History") {
                    event.preventDefault();
                    var $trigger = $(this);
                    $trigger.hide();
                    $.ajax({
                        cache: false,
                        url: href,
                        dataType: "html",
                        success: function (data) {
                            $trigger.show();
                            $('#day-history-content').html(data);
                            $('#day-history').modal();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            AjaxError(url, textStatus, errorThrown)
                        }
                    });
                    if ($hasKnownCheckbox.length > 0) {
                        $hasKnownCheckbox.click();
                    }
                }
                else {
                    if ($hasKnownCheckbox.length > 0) {
                        var url = "/Message/_Read/" + $hasKnownCheckbox.val();
                        $.ajax({
                            url: url,
                            //TODO: don't know why this error is always pop up?
                            //success:function(data){},
                            //error: function (jqXHR, textStatus, errorThrown) {
                            //    AjaxError(url, textStatus, errorThrown)
                            //}
                        });
                    }
                }
            })
        })
    </script>
}